/**
 * 
 */
package org.opencare.ndwc.persistence;

import java.util.Collections;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.opencare.ndwc.model.Event;
import org.opencare.ndwc.model.GlobalEvent;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Nont Banditwong
 * 
 */
@Repository
public class JpaEventDao implements EventDao {

	protected final Log logger = LogFactory.getLog(getClass());
	
	@PersistenceContext
	private EntityManager em;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.opencare.ndwc.service.EventService#find(int)
	 */
	@Override
	public Event find(Long id) {
		// TODO Auto-generated method stub
		// EntityManager em = this.emf.createEntityManager();
		return em.find(Event.class, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.opencare.ndwc.service.EventService#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Event> findAll() {
		// TODO Auto-generated method stub
		// EntityManager em = this.emf.createEntityManager();
		List<?> eventList = em.createQuery(
				"select e FROM Event e order by e.id desc").getResultList();

		return (eventList.isEmpty()) ? Collections.EMPTY_LIST : eventList;
	}

	@Override
	public Long countAllEvent() throws DataAccessException {
		// TODO Auto-generated method stub
		
		Query q = em.createQuery("SELECT COUNT(e) FROM Event e");
		Long result = (Long) q.getSingleResult();
		
		return result;
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Event> findAll(int start, int limit)
			throws DataAccessException {
		// TODO Auto-generated method stub

		logger.debug("findAll(" + start + "," + limit +")");
		
		Query ql = em.createQuery("select e FROM Event e order by e.id desc");
		ql.setFirstResult(start);
		ql.setMaxResults(limit);
				
		List<Event> eventList =  ql.getResultList();
				
		return (eventList.isEmpty()) ? Collections.EMPTY_LIST : eventList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.opencare.ndwc.service.EventService#save(org.opencare.ndwc.model.Event)
	 */

	@Transactional
	@Override
	public void save(Event event) {
		// TODO Auto-generated method stub

		if (event.getId() == null) {
			em.persist(event);
		} else {
			em.merge(event);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.opencare.ndwc.service.EventService#remove(int)
	 */
	@Transactional
	@Override
	public void remove(Long id) throws DataAccessException {
		// TODO Auto-generated method stub

		Event e = find(id);
		if (e != null) {
			em.remove(e);
		}
	}

	@Override
	public void removeAll() throws DataAccessException {
		// TODO Auto-generated method stub
		em.createQuery("delete from Event").executeUpdate();
	}

	@Override
	public Long countAllGlobalEvent() throws DataAccessException {
		// TODO Auto-generated method stub
		Query q = em.createQuery("SELECT COUNT(ge) FROM GlobalEvent ge");
		Long result = (Long) q.getSingleResult();
		
		return result;
	}

	@Override
	public List<GlobalEvent> findAllGlobalEvent(int start, int limit)
			throws DataAccessException {
		// TODO Auto-generated method stub
		logger.debug("findAllGlobalEvent(" + start + "," + limit +")");
		
		Query ql = em.createQuery("select ge FROM GlobalEvent ge order by ge.id desc");
		ql.setFirstResult(start);
		ql.setMaxResults(limit);
				
		List<GlobalEvent> globalEventList =  ql.getResultList();
				
		return (globalEventList.isEmpty()) ? Collections.EMPTY_LIST : globalEventList;
	}



}
